// pages/home/device/setalarmconfig.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cfg_did: "",
    cfg_type: 0,
    cfg_name: "",
    cfg_value:0,
    cfg_value2: 0,
    cfg_enable: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log("value2:"+options.value2);
    this.setData({
      cfg_did: options.did,
      cfg_type: options.type,
      cfg_name: options.name,
      cfg_value: options.value,
      cfg_value2: options.value2,
      cfg_enable: options.enable,
    });
  },

  listerner_value: function (e) {
    this.setData({
      cfg_value: e.detail.value
    })
  },
  listerner_value2: function (e) {
    this.setData({
      cfg_value2: e.detail.value
    })
  },

  listerner_enable: function (e) {
    this.setData({
      cfg_enable: e.detail.value?1:0
    })
  },

  onalarmconfig:function(e){
    var that = this;
    //console.log(this.data);
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/SetAlarm", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: that.data.cfg_did,
        enable: that.data.cfg_enable,
        type: that.data.cfg_type,
        value: that.data.cfg_value,
        value2: that.data.cfg_value2,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data.data);
        var msg = "";
        if (res.data.result) {
          setTimeout(function () { 
            wx.navigateBack();
            }, 2000)
         
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
})